home *** CD-ROM | disk | FTP | other *** search
/ Light ROM 1 / LIGHT-ROM 1 (Amiga Library Services)(1994).iso / ffdisks / d913.lha / PickStartup / pickstartup.c < prev    next >
C/C++ Source or Header  |  1993-10-03  |  17KB  |  535 lines

  1.  
  2. /*  INCLUDES for Intuition  ******************************************** */
  3. ///
  4. #include "exec/types.h"
  5. #include "exec/libraries.h"
  6. #include "exec/memory.h"
  7. #include "libraries/asl.h"
  8. #include "proto/exec.h"
  9.  
  10. #include "clib/intuition_protos.h"
  11. #include "clib/dos_protos.h"
  12. #include "clib/exec_protos.h"
  13. #include "clib/asl_protos.h"
  14. #include "clib/alib_protos.h"
  15.  
  16. #include "devices/keyboard.h"
  17. #include "stdio.h"
  18. #include "string.h"
  19. #include "stdlib.h"
  20. #include "dos/dos.h"
  21. #include "intuition/intuition.h"
  22. #include "intuition/intuitionbase.h"
  23. #include "intuition/screens.h"
  24.  
  25.  
  26. UBYTE *vers = "$VER: PickStartup V1.3";
  27. ///
  28. /* Window defines */
  29. ///
  30. #define MY_WIN_LEFT   0
  31. #define MY_WIN_TOP    0
  32. #define MY_WIN_WIDTH  356
  33. #define MY_WIN_HEIGHT 69
  34. #define MAXPATH    512         /*  path/file buffer max size  */
  35. #define MATRIX_SIZE 16L         /* 2.04 Keyboard Matrix Size */
  36.  
  37. ///
  38. /*************************************************************************
  39. *                        Var Stuff                                       *
  40. *************************************************************************/
  41. ///
  42. struct EasyStruct MyHelp =
  43.       sizeof(struct EasyStruct),
  44.       0,
  45.       "CommanLine Options are:",
  46.       "PickStartup2 [?] [RMB=] [DIR=] [FILTER=] [DELAY=] [KEY=] [F1=] [NOWIN]\n\n"
  47.       "DeFaults are:\n"
  48.       "? NONE unless you ask.\n"
  49.       "KEY=0x63 or commonly known as the Ctrl key.\n"
  50.       "RMB=none\n"
  51.       "F1=none\n"
  52.       "DIR=S:special-startups\n"
  53.       "DELAY=5\n"
  54.       "FILTER=Start-#?\n"
  55.       "NOWIN\n"
  56.       "Example....... \n"
  57.       "Pickstartup RMB=S:test F1=S:Test2 DELAY=7 DIR=S: FILTER=s#?\n"
  58.       "This will execute the s:test if you press the RMB or s:test2 if F1 pressed\n"
  59.       "Or open ASL Req to Dir s: and display all filenames starting with s.\n"
  60.       "If no button pressed the window will remain open for a delay of 7\n"
  61.       "unless NOWIN option used. Then close and continue with your \n"
  62.       "current Startup-sequence.\n"
  63.       "This DELAY time will vary depending on your system\n" ,
  64.       "Thanks|Show some KeyCodes",
  65. };
  66.  
  67. struct EasyStruct SomeKeyCodesHelp =
  68.       sizeof(struct EasyStruct),
  69.       0,
  70.       " Here are some common KeyCodes ",
  71.       "63  CTRL Key  (default) \n"
  72.       "60  Left Shift Key \n"
  73.       "64  Left Alt Key \n"
  74.       "61  Right Shift Key \n"
  75.       "65  Right Alt Key  \n"
  76.       "50-59  F1-F10\n"
  77.       "Don't use 50 if you are using the F1= option\n",
  78.       "Thanks",
  79. };
  80.  
  81. struct IntuiText IText1 =
  82. {
  83.   1, 0, JAM2,                   /* front and back text pens, drawmode and fill byte */
  84.   32, 32,                       /* XY origin relative to container TopLeft */
  85.   NULL,                 /* font pointer or NULL for default */
  86.   "   Click For Startup List ", /* pointer to text */
  87.   NULL                  /* next IntuiText structure */
  88. };
  89.  
  90. struct TagItem win_tags[] =     /* Window Tags  */
  91.  
  92. {
  93.   WA_Left,        MY_WIN_LEFT,
  94.   WA_Top,         MY_WIN_TOP,
  95.   WA_Width,       MY_WIN_WIDTH,
  96.   WA_Height,      MY_WIN_HEIGHT,
  97. /*  WA_DragBar,     TRUE,*/
  98.   WA_RMBTrap,     TRUE,
  99. /*  WA_CloseGadget, TRUE,*/
  100.   WA_Title,       (ULONG) " PickStartup by Bill Pierpont 1993",
  101.   WA_Activate,    TRUE,
  102.   WA_IDCMP,       IDCMP_MOUSEBUTTONS | IDCMP_CLOSEWINDOW | IDCMP_MOUSEBUTTONS | IDCMP_RAWKEY,
  103.   TAG_DONE,       NULL,
  104. };
  105. ///
  106. /* Protos */
  107. ///
  108. void ExecuteFileName(VOID);
  109. void ExecuteF1_FileName(VOID);
  110. void GetStringArg(int, int, TEXT *arg[]);
  111. VOID GetOptions (int argc, TEXT *argv[]);
  112. APTR AllocAslRequest (ULONG, struct TagItem *);
  113. VOID FreeAslRequest (APTR request);
  114. int CloseUpDevice (VOID);
  115. VOID PrintHelp(VOID);
  116. VOID GetFileName (VOID);
  117. VOID Handle_Events (struct Window *);
  118. BOOL AddPart (UBYTE * dirname, UBYTE * filename, unsigned long size);
  119. BOOL IsKeyDn (LONG);
  120.  
  121. ///
  122. /***********************************************************************
  123. *                               Globals                                *
  124. ************************************************************************/
  125. ///
  126. LONG KeyToCk = 0x63;
  127. LONG F1_Key = 0x50;
  128. LONG oslibversion = 37;
  129. ULONG LoopCount = 50000;
  130. USHORT code = NULL;
  131.  
  132. BOOL NoWin = FALSE; 
  133. BOOL Continue = TRUE;
  134. BOOL Cancelled = FALSE;
  135. BOOL Check_F1 = FALSE;
  136.  
  137. TEXT Default_Buffer[MAXPATH] = "";
  138. TEXT WorkBuf[MAXPATH] = "";
  139. TEXT F1_Buffer[MAXPATH] = "";
  140. TEXT FilePath[MAXPATH] = "s:special-startups";  /* Buffer For File request pathname    */
  141. TEXT FilePattern[MAXPATH] = "start-#?";      
  142.  
  143.  
  144. extern struct Library *IntuitionBase;
  145. extern struct Library *AslBase;
  146.  
  147. int count = 0;
  148. ///
  149.  
  150. /***********************************************************************
  151. *                            main                                      *
  152. ***********************************************************************/
  153.  
  154. int 
  155. main(int argc, TEXT *argv[])
  156. {
  157. ///
  158.  struct Window *win;  /// get options if any
  159.   if (argc > 0)
  160.     {GetOptions (argc, argv);}
  161.  
  162. if (!Continue)      /// if we found the help option quit here 
  163.     { return (0); }
  164.   else
  165.     { Continue = FALSE; }  /// reset switch                       
  166.  
  167. if (IsKeyDn(KeyToCk))
  168.   {
  169.        GetFileName ();
  170.        if (Cancelled==TRUE)  /// User Selected cancel duuh
  171.            { return(0);}
  172.           else   
  173.            { return(10); }  /// User selected File exit and kill Cli
  174.    }
  175. if (Check_F1)  // If F1 Option used do this
  176.       {  
  177.          KeyToCk = 0x50;   // 50 hex is Key Code for F1_Key         
  178.       
  179.       if (IsKeyDn(KeyToCk))
  180.               {    ExecuteF1_FileName();               /// yep its down
  181.                        if (Cancelled)                  /// Close if Cancelled Selected 
  182.                          {  return (0);}               /// and quit but don't Kill current Startup
  183.                        else
  184.                           { return (10);}       
  185.                  }
  186.               }
  187. if(NoWin)              
  188.  { return(0);}
  189. else
  190.       {
  191.         win = OpenWindowTagList (NULL, win_tags); /// open the window and print 
  192.               if (win == NULL)                          /// and print some text      
  193.                       { printf ("Window Failed to Open"); }
  194.               else
  195.                       { PrintIText (win->RPort, &IText1, 0, 0);
  196.                         Handle_Events (win);
  197.                         CloseWindow (win);                 /// you can close that window now  
  198.                        }
  199.  
  200.  /// After calling Handle_Events the 
  201.  ///Continue = code MENUDOWN or MOUSEBUTTONS
  202.  
  203.    if (code)                                  /// If window clicked code is set
  204.         {      ExecuteFileName();             /// If not set window timed out
  205.               if (Cancelled)                  /// Close if Cancelled Selected 
  206.                 {  return (0);}                /// and quit but don't Kill current Startup
  207.               else
  208.                 { return (10);}            /// job done quit and kill startup-sequence 
  209.         }
  210.       }
  211.    return (0);
  212. ///
  213. }
  214.  
  215. /***************************************************************************
  216. GetFileName()
  217.  
  218. Calls ASLreq.
  219. Get the users input.
  220. Then execute the selected script.
  221. ****************************************************************************/
  222.  
  223. void 
  224. GetFileName ()
  225. {
  226. ///
  227.   struct FileRequester *fr;
  228.   TEXT buffer[MAXPATH];
  229.  
  230.   if (AslBase = OpenLibrary ("asl.library", 37L))
  231.     {
  232.       if (fr = (struct FileRequester *) AllocAslRequestTags (ASL_FileRequest,
  233.  
  234.                         ASL_Hail, (ULONG) "Pick Startup File",
  235.                         ASL_Height, 200,
  236.                         ASL_Width,  220,
  237.                         ASL_LeftEdge, 0,
  238.                         ASL_TopEdge, 0,
  239.                         ASL_OKText, (ULONG) "OK",
  240.                         ASL_CancelText, (ULONG) "Cancel",
  241.                         ASL_Pattern, (TEXT *) &FilePattern,
  242.                         ASL_FuncFlags, FILF_PATGAD,
  243.                         ASL_Dir, (TEXT *) &FilePath,
  244.                         TAG_DONE))
  245.         {
  246.           if (AslRequest (fr, NULL))              /* This Call Opens the Requester*/
  247.             {
  248.               strcpy (buffer, "execute ");        /* Build PathName */
  249.               strcat (buffer, fr->rf_Dir);      
  250.               AddPart (buffer, fr->rf_File, sizeof (buffer) 
  251.                      - strlen (buffer));
  252.  
  253.               if(strncmp(fr->rf_File, "",1))    /*if not a null string execute it */
  254.               {Execute ((STRPTR) &buffer, NULL, NULL);}
  255.  
  256.             }
  257.           else
  258.             {
  259.               Cancelled = TRUE;
  260.             }
  261.         }       /* end if */
  262.  
  263.  
  264.       FreeAslRequest (fr);
  265.       CloseLibrary (AslBase);
  266.  
  267.     }
  268.   else
  269.     {
  270.       printf ("You Need WorkBench Version 2.04 to run this Program");
  271.     }
  272. ///
  273. }
  274.  
  275. /***********************************************************************
  276. Handle_Events()
  277.  
  278. Does what its name implies
  279. until LoopCount == 0 or user hits a mouse botton
  280. LoopCount can be changed by command line option.
  281.  
  282. I know busyloop ......badboy but becaues of the nature of the program
  283. since it isn't multitasking at this point it dosn't make any difference.
  284. Someday I hope to learn how to use Timer Device.
  285. ************************************************************************/
  286.  
  287. void 
  288. Handle_Events (struct Window *win)
  289. {
  290. ///
  291.   
  292.   ULONG class;
  293.   BOOL Done = FALSE;
  294.   struct IntuiMessage *message;
  295.   register long windowsig = 0L;
  296.   register struct MsgPort *Iport = NULL;
  297.   Iport = win->UserPort;
  298.   windowsig = 1L << Iport->mp_SigBit;
  299.      
  300.   while (!Done)
  301.     {
  302.       count++;
  303.       if (count > LoopCount)
  304.         Done = TRUE;
  305.  
  306.       while ((!Done) &&
  307.              (message = (struct IntuiMessage *) GetMsg (win->UserPort)))
  308.         {
  309.  
  310.           class = message->Class;
  311.           code = message->Code;
  312.           ReplyMsg ((struct Message *) message);
  313.  
  314.           if (class == IDCMP_MOUSEBUTTONS)
  315.             {
  316.               Done = TRUE;
  317.               Continue = TRUE;
  318.             }
  319.             else 
  320.             {
  321.               code = NULL;  /// clear code so main() does not 
  322.                             ///open ASL when we return
  323.             }
  324.           }
  325.        }
  326.   }
  327. /****************************************************************************
  328. GetOptions()
  329.  
  330. Sets up options recieved from the command line
  331.  Loop thought the argv array looking for strings that match KeyWords. 
  332.  If one is found then get and seperate the KeyWord from the rest of the 
  333.  string to get parameter the user entered. 
  334. *****************************************************************************/
  335. VOID 
  336. GetOptions (int argc, TEXT *argv[])
  337. {
  338. ///
  339.   int  length;
  340.   int count;
  341.   int temphexa;
  342.    
  343.    for (count = 1; count < argc; count++)
  344.     {    length=stclen(argv[count]);
  345.                                     
  346.       if(!strncmp(argv[count], "?",1))
  347.           {
  348.               PrintHelp();
  349.               Continue = FALSE;
  350.            }
  351.       if(!strncmp(argv[count], "NOWIN",5))
  352.           {
  353.               NoWin = TRUE;
  354.             ///  Continue = FALSE;
  355.            }
  356.  
  357.         if(!strncmp(argv[count],"FILTER=",7))
  358.              {        
  359.                   GetStringArg(8,length,&argv[count]);
  360.                   strcpy (FilePattern, WorkBuf);  
  361.              }
  362.         if(!strnicmp(argv[count], "KEY=",4))
  363.              {  
  364.                   GetStringArg(5,length,&argv[count]);
  365.                   stch_i(WorkBuf,&temphexa); /* Grrr, why isn't it like atoi?! */
  366.                   KeyToCk = temphexa ? (UWORD)temphexa : 0x63;
  367.               } 
  368.         if(!strncmp(argv[count],"RMB=",4))
  369.              {        
  370.                   GetStringArg(5,length,&argv[count]);
  371.                   if(length >4)        /* if  4 we have a string */
  372.                   {
  373.                     strcpy (Default_Buffer, "execute "); /* set up command else we use the default */
  374.                     strcat (Default_Buffer, WorkBuf);
  375.                   }  
  376.              }
  377.          if(!strncmp(argv[count],"F1=",3))
  378.              {        
  379.                 GetStringArg(4,length,&argv[count]);
  380.                 
  381.                 if(length >3)             // if  3 we have a string */
  382.                   {
  383.                     strcpy (F1_Buffer, "execute "); // set up command else we use the default */
  384.                     strcat (F1_Buffer, WorkBuf);
  385.                     Check_F1 = TRUE;
  386.                   }  
  387.              }
  388.  
  389.        if(!strncmp(argv[count], "DELAY=",6))
  390.              {  
  391.                       GetStringArg(7,length,&argv[count]);  
  392.                   if((LoopCount = strtoul(WorkBuf,NULL,10)*10000) == 0)
  393.                      {LoopCount = 5000;} 
  394.              } 
  395.        if(!strncmp(argv[count],"DIR=",4))
  396.              {        
  397.                   GetStringArg(5,length,&argv[count]);
  398.                     if(length > 4)
  399.                     {
  400.                       strcpy (FilePath, WorkBuf);
  401.                      }  
  402.               }
  403.                                       
  404.     }
  405. ///
  406. }
  407.  
  408. /****************************************************************
  409. * get the substring of the argument                             *
  410. * I really just wrote this routine to practice passing arguments*
  411. * call GetStringArg(p,l,argv)                               
  412. * p = int = to the length of the keyword+1 e.g "DIR=" is 5 
  413. * l = length of argv string (already calced.
  414. * argv = address of argv[count] string
  415. *****************************************************************/
  416.  
  417. void GetStringArg(int ParameterLength,int length,TEXT *argv[])
  418. {
  419.  ///
  420.   struct EasyStruct CommandErrorHelp =
  421.    {
  422.       sizeof(struct EasyStruct),
  423.       0,
  424.       "HELLO, You Need Help with PickStartup2",
  425.       "You have an Error in your command line Option for \n"
  426.       "%s\n"
  427.       "Do Not put a space between the Keyword and your option.\n\n"
  428.       "I will use the default value for now. ",
  429.       "OK|Show Me How",
  430. };
  431.  
  432.    
  433.        if(ParameterLength-1 == length)
  434.               { 
  435.             if(!EasyRequestArgs(NULL,&CommandErrorHelp,NULL,argv))        /* if show me how selected printHelp */
  436.                       {PrintHelp();}
  437.               }
  438.                else
  439.                { strmid(*argv,WorkBuf,ParameterLength,length);}    /* Gets the SUbstring and puts it in WorkBuf */
  440. ///
  441. }
  442. /**************************************************************************
  443. PrintHelp()
  444.  
  445. Calls EasyRequest to display some help. 
  446. Like you couldn't tell this.
  447. ***************************************************************************/
  448. void PrintHelp()
  449. {
  450.  if(!EasyRequest(NULL,&MyHelp,NULL,"(var)",2))
  451.        EasyRequest(NULL,&SomeKeyCodesHelp,NULL,"(var)",2);
  452. }
  453. ///
  454. /*************************************************************************
  455. IsKeyDn()
  456.  
  457. /* This Section of code supplied by J.Luk. */
  458.  
  459. Keyboard.device checking if a certain key is down
  460. ARGS: UWORD KeyToCk -- Raw Key Matrix code, see RKM:Libs p707(1.3),828(2.04)
  461. RESULT: BOOL of whether or not the key is down!  (sheesh)
  462. **************************************************************************/
  463.  
  464. BOOL IsKeyDn(KeyToCk)
  465. LONG KeyToCk;
  466. {
  467.    struct IOStdReq *keyRequest;
  468.    struct MsgPort *keyPort;
  469.    UBYTE *keyMatrix;
  470.  
  471.    BOOL RetFlg = FALSE;
  472.    
  473.    if (keyPort=CreatePort(NULL,NULL) )
  474.    {
  475.       if (keyRequest=(struct IOStdReq *)CreateExtIO (keyPort, sizeof (struct IOStdReq)))
  476.       {
  477.          if (!OpenDevice("keyboard.device",NULL,(struct IORequest *) keyRequest, NULL) )
  478.          {
  479.             if (keyMatrix=AllocMem(MATRIX_SIZE, MEMF_PUBLIC | MEMF_CLEAR) )
  480.             {
  481.                keyRequest->io_Command=KBD_READMATRIX;
  482.                keyRequest->io_Data=(APTR)keyMatrix;
  483.                keyRequest->io_Length=13;
  484.                
  485.                DoIO ( (struct IORequest *) keyRequest);
  486.                                                                                                 
  487.                /* Why couldn't the RKM just say this?!!!! */
  488.                /* No Shit ! */
  489.  
  490.                if (keyMatrix[KeyToCk/8] & (1L<<(KeyToCk%8))) RetFlg = TRUE;
  491.             
  492.                /* Access the byte of the key matrix, and test to see if */
  493.                /* bit KeyToCk%8 (the remainder) is set */
  494.  
  495.                FreeMem (keyMatrix, MATRIX_SIZE);
  496.             }
  497.             CloseDevice ( (struct IORequest *) keyRequest);
  498.          }
  499.          DeleteExtIO ( (struct IORequest *) keyRequest);
  500.       }      DeletePort (keyPort);
  501.    }
  502.    return (RetFlg);
  503. }
  504.  
  505.  
  506. void ExecuteFileName()
  507. {
  508.  
  509.  if(code == MENUDOWN)  /// if RMB do the default command line file only option 
  510.      {
  511.         if(strcmp(Default_Buffer,"")) /// setup up the file name from the commandline to execute 
  512.           { Execute ((STRPTR) &Default_Buffer, NULL, NULL); }
  513.          else 
  514.            { GetFileName ();}/* RMB file name entered wrong, call ASL and get one */
  515.         }   
  516.    else
  517.      { 
  518.       GetFileName(); } 
  519. }
  520. void ExecuteF1_FileName()
  521. {
  522.      
  523.  if(Check_F1)  /// if RMB do the default command line file only option 
  524.      {
  525.  
  526.         if(strcmp(F1_Buffer,"")) /// setup up the file name from the commandline to execute 
  527.           { Execute ((STRPTR) &F1_Buffer, NULL, NULL); }
  528.          else 
  529.            { GetFileName ();}/* F1 file name entered wrong, call ASL and get one */
  530.         }   
  531.    else
  532.      { GetFileName(); } 
  533. }